package cn.iocoder.yudao.module.book.service.collections;

import cn.iocoder.yudao.module.book.controller.app.booksShell.vo.AppCollectionsRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.book.controller.admin.collections.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.collections.CollectionsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.book.dal.mysql.collections.CollectionsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.*;

/**
 * 绘本集 Service 实现类
 *
 * @author xi
 */
@Service
@Validated
public class CollectionsServiceImpl implements CollectionsService {

    @Resource
    private CollectionsMapper collectionsMapper;

    @Override
    public Integer createCollections(CollectionsSaveReqVO createReqVO) {
        // 插入
        CollectionsDO collections = BeanUtils.toBean(createReqVO, CollectionsDO.class);
        collectionsMapper.insert(collections);
        // 返回
        return collections.getCollectionId();
    }

    @Override
    public void updateCollections(CollectionsSaveReqVO updateReqVO) {
        // 校验存在
        validateCollectionsExists(updateReqVO.getCollectionId());
        // 更新
        CollectionsDO updateObj = BeanUtils.toBean(updateReqVO, CollectionsDO.class);
        collectionsMapper.updateById(updateObj);
    }

    @Override
    public void deleteCollections(Integer id) {
        // 校验存在
        validateCollectionsExists(id);
        // 删除
        collectionsMapper.deleteById(id);
    }

    private void validateCollectionsExists(Integer id) {
        if (collectionsMapper.selectById(id) == null) {
            throw exception(COLLECTIONS_NOT_EXISTS);
        }
    }

    @Override
    public CollectionsDO getCollections(Integer id) {
        return collectionsMapper.selectById(id);
    }

    @Override
    public PageResult<CollectionsDO> getCollectionsPage(CollectionsPageReqVO pageReqVO) {
        return collectionsMapper.selectPage(pageReqVO);
    }

    @Override
    public AppCollectionsRespVO selectByLang(Integer collectionId,Integer lang) {
        return collectionsMapper.selectByLang(collectionId,lang);
    }

    @Override
    public Boolean markGroupTop(Integer id) {
        return collectionsMapper.updateIsTop(id) > 0;
    }

    @Override
    public List<AppCollectionsRespVO> selectAll() {
        return collectionsMapper.selectAll();
    }

    @Override
    public List<AppCollectionsRespVO> selectAllByLang(Integer lang) {
        return collectionsMapper.selectAllByLang(lang);
    }

    @Override
    public Integer getCategoryId(Integer id) {
        return collectionsMapper.getCategoryId(id);
    }

    @Override
    public List<AppCollectionsRespVO> selectLikeContent(String content) {
        return collectionsMapper.selectLikeContent(content);
    }

}